home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Mark Pilgrim / Startup Screen Picker 1.2 / source / ssp INIT ƒ / ssp code ƒ / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-30  |  5.4 KB  |  176 lines  |  [TEXT/KAHL]

  1. /**********************************************************************\
  2.  
  3. File:        main.c
  4.  
  5. Purpose:    This module handles all the file moving to and from the
  6.             "Startup Screens" folder.
  7.             
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12.  
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with this program in a file named "GNU General Public License".
  20. If not, write to the Free Software Foundation, 675 Mass Ave,
  21. Cambridge, MA 02139, USA.
  22.  
  23. \**********************************************************************/
  24.  
  25. #include "main.h"
  26. #include "init.h"
  27. #include "prefs.h"
  28. #include "Folders.h"
  29.  
  30. unsigned char        gLastName[32];
  31.  
  32. short DoSSThing(void)
  33. {
  34.     OSErr            isHuman;
  35.     CInfoPBRec        pb_file, pb_dir;
  36.     HParamBlockRec    pb_basic, pb_new;
  37.     CMovePBRec        pb_move;
  38.     short            vRefNum;
  39.     long            parID;
  40.     short            i;
  41.     long            temp;
  42.     unsigned char    newName[32];
  43.     Boolean            gotone;
  44.     
  45.     isHuman=FindFolder(kOnSystemDisk, kSystemFolderType, kDontCreateFolder, &vRefNum,
  46.                 &parID);                    /* find system folder */
  47.     if (isHuman!=noErr)
  48.         return cantFindSystemFolderErr;
  49.     
  50.     pb_dir.dirInfo.ioCompletion=0L;
  51.     pb_dir.dirInfo.ioNamePtr="\pStartup Screens";
  52.     pb_dir.dirInfo.ioVRefNum=vRefNum;
  53.     pb_dir.dirInfo.ioFDirIndex=0; /* very important */
  54.     pb_dir.dirInfo.ioDrDirID=parID;
  55.     isHuman=PBGetCatInfo(&pb_dir, FALSE);    /* get info on "Startup Screens" dir */
  56.     if (isHuman!=noErr)
  57.         return cantGetDirInfoErr;
  58.     
  59.     if (pb_dir.dirInfo.ioDrNmFls==0)        /* any files in "Startup Screens" dir? */
  60.         return noFilesErr;
  61.     
  62.     gotone=FALSE;
  63.     for (i=0; ((i<pb_dir.dirInfo.ioDrNmFls) && (!gotone)); i++)
  64.     {
  65.         pb_new.fileParam.ioCompletion=0L;
  66.         pb_new.fileParam.ioNamePtr=newName;
  67.         pb_new.fileParam.ioVRefNum=vRefNum;
  68.         pb_new.fileParam.ioFDirIndex=i+1;
  69.         pb_new.fileParam.ioDirID=pb_dir.dirInfo.ioDrDirID;
  70.         isHuman=PBGetCatInfo(&pb_new, FALSE);
  71.         if (isHuman!=noErr)
  72.             return cantGetNewFileInfoErr;
  73.         if (!(pb_new.fileParam.ioFlFndrInfo.fdFlags&fInvisible))
  74.             gotone=TRUE;    /* found visible file in "Startup Screens" folder */
  75.     }
  76.     
  77.     if (!gotone)
  78.         return noFilesErr;
  79.     
  80.     do
  81.     {
  82.         pb_new.fileParam.ioCompletion=0L;
  83.         pb_new.fileParam.ioNamePtr=newName;
  84.         pb_new.fileParam.ioVRefNum=vRefNum;
  85.         GetDateTime(&temp);
  86.         pb_new.fileParam.ioFDirIndex=(temp&0x7fffffff)%pb_dir.dirInfo.ioDrNmFls+1;
  87.         pb_new.fileParam.ioDirID=pb_dir.dirInfo.ioDrDirID;
  88.         isHuman=PBGetCatInfo(&pb_new, FALSE);    /* get info on random file in */
  89.         if (isHuman!=noErr)                        /* "Startup Screens dir */
  90.             return cantGetNewFileInfoErr;
  91.     }
  92.     while (pb_new.fileParam.ioFlFndrInfo.fdFlags&fInvisible);
  93.     
  94.     pb_basic.fileParam.ioCompletion=0L;
  95.     pb_basic.fileParam.ioNamePtr=gLastName;
  96.     pb_basic.fileParam.ioVRefNum=vRefNum;
  97.     pb_basic.fileParam.ioDirID=parID;
  98.     pb_basic.fileParam.ioFDirIndex=0;
  99.     isHuman=PBGetCatInfo(&pb_basic, FALSE);
  100.     if (isHuman!=noErr)
  101.     {
  102.         pb_basic.fileParam.ioFlFndrInfo.fdFlags&=~0x0100;    /* clear 'initted' flag */
  103.         pb_basic.fileParam.ioCompletion=0L;
  104.         pb_basic.fileParam.ioNamePtr=gLastName;
  105.         pb_basic.fileParam.ioVRefNum=vRefNum;
  106.         pb_basic.fileParam.ioDirID=parID;
  107.         pb_basic.fileParam.ioFDirIndex=0;
  108.         PBSetCatInfo(&pb_basic, FALSE);
  109.     }
  110.     
  111.     pb_basic.fileParam.ioCompletion=0L;
  112.     pb_basic.fileParam.ioNamePtr="\pStartupScreen";
  113.     pb_basic.fileParam.ioVRefNum=vRefNum;
  114.     pb_basic.ioParam.ioMisc=gLastName;
  115.     pb_basic.fileParam.ioDirID=parID;
  116.     isHuman=PBHRename(&pb_basic, FALSE);    /* rename "StartupScreen" to saved name */
  117.     if ((isHuman!=noErr) && (isHuman!=fnfErr))
  118.         return cantRenameStartupScreenErr;
  119.     
  120.     if (isHuman!=fnfErr)
  121.     {
  122.         pb_move.ioCompletion=0L;
  123.         pb_move.ioNamePtr=gLastName;
  124.         pb_move.ioVRefNum=vRefNum;
  125.         pb_move.ioNewName=0L;
  126.         pb_move.ioNewDirID=pb_dir.dirInfo.ioDrDirID;
  127.         pb_move.ioDirID=pb_basic.fileParam.ioDirID;
  128.         isHuman=PBCatMove(&pb_move, FALSE);    /* move old one into "Startup Screens" dir */
  129.         if (isHuman!=noErr)
  130.             return cantMoveStartupScreenErr;
  131.     }
  132.     
  133.     pb_basic.fileParam.ioCompletion=0L;
  134.     pb_basic.fileParam.ioVRefNum=vRefNum;
  135.     pb_basic.fileParam.ioNamePtr=newName;
  136.     pb_basic.fileParam.ioDirID=pb_dir.dirInfo.ioDrDirID;
  137.     pb_basic.fileParam.ioFDirIndex=0;
  138.     isHuman=PBGetCatInfo(&pb_basic, FALSE);
  139.     if (isHuman!=noErr)
  140.     {
  141.         pb_basic.fileParam.ioFlFndrInfo.fdFlags&=~0x0100;    /* clear 'initted' flag */
  142.         pb_basic.fileParam.ioCompletion=0L;
  143.         pb_basic.fileParam.ioVRefNum=vRefNum;
  144.         pb_basic.fileParam.ioNamePtr=newName;
  145.         pb_basic.fileParam.ioDirID=pb_dir.dirInfo.ioDrDirID;
  146.         pb_basic.fileParam.ioFDirIndex=0;
  147.         PBSetCatInfo(&pb_basic, FALSE);
  148.     }
  149.     
  150.     pb_move.ioCompletion=0L;
  151.     pb_move.ioNamePtr=newName;
  152.     pb_move.ioVRefNum=vRefNum;
  153.     pb_move.ioNewName=0L;
  154.     pb_move.ioNewDirID=parID;
  155.     pb_move.ioDirID=pb_dir.dirInfo.ioDrDirID;
  156.     isHuman=PBCatMove(&pb_move, FALSE);        /* move new one into system folder */
  157.     if (isHuman!=noErr)
  158.         return cantMoveNewFileErr;
  159.     
  160.     pb_basic.fileParam.ioCompletion=0L;
  161.     pb_basic.fileParam.ioNamePtr=newName;
  162.     pb_basic.fileParam.ioVRefNum=vRefNum;
  163.     pb_basic.ioParam.ioMisc="\pStartupScreen";
  164.     pb_basic.fileParam.ioDirID=parID;
  165.     isHuman=PBHRename(&pb_basic, FALSE);    /* rename new one to "StartupScreen" */
  166.     if (isHuman!=noErr)
  167.         return cantRenameNewFileErr;
  168.     
  169.     for (i=newName[0]; i>=0; i--)
  170.         gLastName[i]=newName[i];
  171.     
  172.     SaveThePrefs();
  173.     
  174.     return allsWell;
  175. }
  176.